package cn.xorange.commons.system.controller.platform;

import cn.xorange.commons.core.basic.bean.PageBean;
import cn.xorange.commons.core.basic.bean.ResultBean;
import cn.xorange.commons.core.basic.controller.BaseController;
import cn.xorange.commons.logs.annotation.Log;
import cn.xorange.commons.logs.enums.BusinessType;
import cn.xorange.commons.security.annotation.Authorize;
import cn.xorange.commons.security.utils.SecurityUtils;
import cn.xorange.commons.system.entity.SysConfig;
import cn.xorange.commons.system.entity.platform.SysDictType;
import cn.xorange.commons.system.service.platform.ISysDictTypeService;
import cn.xorange.commons.utils.poi.excel.EasyExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/platform/dict/type")
@Authorize(hasPerm = "platform:dict")
public class SysDictTypeController extends BaseController
{
    @Autowired
    private ISysDictTypeService dictTypeService;

    @GetMapping("/list")
    @Authorize(hasPerm = "list")
    public PageBean list(SysDictType dictType)
    {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return new PageBean(list);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Authorize(hasPerm = "export")
    public void export(HttpServletResponse response, SysDictType dictType)
    {
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        EasyExcelUtil.exportExcel(list,"字典类型","字典类型", SysConfig.class,"导出_字典类型",response);
    }

    /**
     * 查询字典类型详细
     */
    @GetMapping(value = "/{dictId}")
    @Authorize(hasPerm = "query")
    public ResultBean getInfo(@PathVariable Long dictId)
    {
        return ResultBean.onSuccess(dictTypeService.selectDictTypeById(dictId));
    }

    /**
     * 新增字典类型
     */
    @Authorize(hasPerm = "add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultBean add(@Validated @RequestBody SysDictType dict)
    {
        if (!dictTypeService.checkDictTypeUnique(dict))
        {
            return ResultBean.onFail("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        return ResultBean.onSuccess(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @Authorize(hasPerm = "edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultBean edit(@Validated @RequestBody SysDictType dict)
    {
        if (!dictTypeService.checkDictTypeUnique(dict))
        {
            return ResultBean.onFail("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        return ResultBean.onSuccess(dictTypeService.updateDictType(dict));
    }

    /**
     * 删除字典类型
     */
    @Authorize(hasPerm = "remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictIds}")
    public ResultBean remove(@PathVariable Long[] dictIds)
    {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return ResultBean.onSuccess();
    }

    /**
     * 刷新字典缓存
     */
    @Authorize(hasPerm = "remove")
    @Log(title = "字典类型", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public ResultBean refreshCache()
    {
        dictTypeService.resetDictCache();
        return ResultBean.onSuccess();
    }

    /**
     * 获取字典选择框列表
     */
    @GetMapping("/optionSelect")
    public ResultBean optionselect()
    {
        List<SysDictType> dictTypes = dictTypeService.selectDictTypeAll();
        return ResultBean.onSuccess(dictTypes);
    }
}
